package cn.jobstart.data.liePin;

import cn.jobstart.common.FileUtil;
import cn.jobstart.common.SkList;
import cn.jobstart.common.StringUtil;
import cn.jobstart.props.FileConfig;
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.File;
import java.util.List;

/**
 * @author sunke
 * @version 1.0
 * @Description
 * @date 2023-12-07 10:39
 */

@Component
public class JobTitleUtil {
    @Resource
    private FileConfig fileConfig;


    private List<JobTitle> jobTitleList;


    private void init() {
        try {
            String data = FileUtil.readInFile(fileConfig.getRoot() + File.separator + "lie_pin" + File.separator + "job_title.js", "utf-8");

            JSONObject jsonObject = JSONObject.parseObject(data);

            jobTitleList= SkList.getInstance();
            for (String code : jsonObject.keySet()) {
                try {

                    JSONObject item = jsonObject.getJSONObject(code);
                    JobTitle jobTitle = new JobTitle();
                    jobTitle.setChName(item.getString("chName"));
                    jobTitle.setShortName(item.getString("shortName"));
                    jobTitle.setCode(item.getString("code"));
                    jobTitle.setParentCode(item.getString("parentCode"));
                    jobTitleList.add(jobTitle);
                } catch (Exception e) {

                }

            }


        } catch (Exception e) {

        }

    }

    public String getCodeByName(String name) {

        if (StringUtil.isNullOrEmpty(name)) {
            return null;
        }

        if (StringUtil.isNullOrEmpty(jobTitleList)) {
            init();
        }
        if (StringUtil.isNullOrEmpty(jobTitleList)) {
            return null;
        }

        for (JobTitle jobTitle : jobTitleList) {

            if (StringUtil.isNullOrEmpty(jobTitle.getChName())) {
                continue;
            }

            if (name.equalsIgnoreCase(jobTitle.getChName())) {
                return jobTitle.getCode();
            }

        }


        return "";
    }


}
